package cn.yunhe.action;

import cn.yunhe.entity.Page;
import cn.yunhe.entity.Person;
import cn.yunhe.service.IPersonService;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import org.apache.struts2.ServletActionContext;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.HashMap;
import java.util.Map;

@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
public class PersonAction extends ActionSupport implements ModelDriven<Person> {
    private Person person;
    private Page page;

    private int pageSize;
    private int pageNum;


    @Autowired
    private IPersonService personService;

    @Override
    public String execute() {
        Map<String, Object> cond = new HashMap<>();
        cond.put("pageSize", pageSize);
        cond.put("pageNum", pageNum);
        cond.put("person", person);
        page = personService.queryLikePerson(cond);
        return SUCCESS;
    }


    @Override
    public Person getModel() {
        if (person == null) {
            person = new Person();
        }
        return person;
    }


    public String add() {
        personService.addPerson(person);
        return SUCCESS;
    }

    public String del() {
        personService.delPerson(person);
        return SUCCESS;
    }

    public String getById() {
        Person persons = personService.getPersonById(person);
        ServletActionContext.getRequest().setAttribute("person", persons);
        return "person";
    }


    public String update() {
        personService.updatePerson(person);
        System.out.println(1);
        return SUCCESS;
    }
}
